/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sgt.supermarket.controller.admin;

import java.io.IOException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sgt.supermarket.controller.XServlet;
import sgt.supermarket.entity.Employee;
import sgt.supermarket.entity.Sex;
import sgt.supermarket.hibernatedao.HibernateDAOFactory;
import sgt.supermarket.util.Converter;
import sgt.supermarket.util.ServletExtensions;

/**
 *
 * @author Administrator
 */
public class EditEmployee extends XServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        HibernateDAOFactory factory = new HibernateDAOFactory();
        String username = ServletExtensions.getStringParam(request, "u");
        Employee employee = null;
        if (username.isEmpty()) {
            employee = new Employee();
        } else {
            employee = factory.getEmployeeDAO().getByUsername(username);
        }
        if (request.getMethod().equalsIgnoreCase("POST")) {
            if (employee.getAutoId() == 0) {
                employee.setUsername(request.getParameter("username"));

            }
            employee.setFullname(request.getParameter("fullname"));
            employee.setPassword(request.getParameter("password"));
            employee.setSex(Sex.valueOf(request.getParameter("sex")));

            String dateOfBirth = request.getParameter("dateOfBirth") + "/" + request.getParameter("dateOfBirth-mm") + "/" + request.getParameter("dateOfBirth-dd");
            employee.setDateOfBirth(Converter.toDate(dateOfBirth, "yyyy/M/d"));
            employee.setEnabled(ServletExtensions.getBooleanParam(request, "enabled"));
            if (!request.getParameter("password").equals(request.getParameter("passwordAgain"))) {
                request.setAttribute("msg", "Password does not match");
                request.setAttribute("passwordAgain", request.getParameter("passwordAgain"));
            } else {
                factory.getEmployeeDAO().save(employee);
                response.sendRedirect(request.getContextPath() + "/admin/listEmployees");
                return;
            }
        }
        request.setAttribute("title", employee.getAutoId() != 0 ? "Edit employee: " + employee.getFullname() : "Create new employee");
        request.setAttribute("employee", employee);
        request.getRequestDispatcher("/admin.editEmployee.tiles").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(EditEmployee.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(EditEmployee.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
